#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 110;

LL n, k, d;
LL A[N];
LL f[N][N][N];



int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k >> d;
    for(int i = 1; i <= n; i ++) cin >> A[i];

    for(int i = 0; i <= n; i ++){
        for(int j = 0; j <= k; j ++){
            for(int p = 0; p <= d; p ++){
                f[i][j][p] = -1;
                if(j == 0 && p == 0) f[i][j][p] = 0;
            }
        }
    }


    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= k; j ++){
            for(int p = 0; p < d; p ++){
                f[i][j][p] = max(f[i][j][p], f[i - 1][j][p]);
                if(f[i - 1][j - 1][((p - A[i]) % d + d) % d] == -1) continue;
                f[i][j][p] = max(f[i][j][p], f[i - 1][j - 1][((p - A[i]) % d + d) % d] + A[i]);

            }
        }
    }


    cout << f[n][k][0] << '\n';


    return 0;
}